package com.hnkywl.jip.service;

import com.hnkywl.jip.dao.ReceiptPaymentDao;
import com.hnkywl.jip.interceptor.AuthInterceptor;
import com.hnkywl.jip.utils.VoucherItemData;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 说明：
 * 包:        com.hnkywl.jip.service
 * 类描述:    收付款服务
 * 创建人:    ZHUCHG
 * 创建时间: 13-2-20 下午4:03
 * 修改人:    ZHUCHG
 * 修改时间: 13-2-20 下午4:03
 * 修改备注: [说明本次修改内容]
 * 版本:        [v1.0]
 */
@Service
public class ReceiptPaymentService {
    private ReceiptPaymentDao receiptPaymentDao;

    /**
     * @param type       收款/付款
     * @param startTime  开始时间
     * @param endTime    结束时间
     * @param customId   客户ID
     * @param billNumber 票据号
     * @param companyId  公司ID
     * @param voucherId
     * @param operator   操作人
     * @return
     */
    @Transactional
    public Page<Object[]> findReceiptPayment(Integer type, Date startTime, Date endTime, Integer customId,
                                             String billNumber, Integer companyId, String voucherId, String operator, Integer status, Integer limit, Integer start) {
        return receiptPaymentDao.findReceiptPayment(type, startTime, endTime, customId, billNumber, companyId, voucherId, operator, status, limit, start);
    }

    @Transactional
    public Map getVoucher(Integer id, String uuid, List<VoucherItemData> voucherItemDataList) {
        Map<String, Object> result = new HashMap<String, Object>();
        List<Object[]> list = receiptPaymentDao.getVoucher(id);
        //凭证分录信息
        List<Map> items = new ArrayList<Map>();
        int order = 1;
        for (Object[] item : list) {
            Map<String, Object> voucherItem = new HashMap<String, Object>();
            voucherItem.put("id", order);//唯一标识
            voucherItem.put("vchrid", uuid);//凭证主表ID
            voucherItem.put("itemorder", order);//分录编号
            voucherItem.put("subjectcode", item[1] != null ? item[1] : "");//科目代码
            voucherItem.put("subjectname", item[2] != null ? item[2] : "");//科目名称
            voucherItem.put("debit", item[3] != null ? item[3].toString() : "");//借方金额
            voucherItem.put("credit", item[4] != null ? item[4].toString() : "");//贷方金额
            voucherItem.put("qty", "0");//数量
            voucherItem.put("price", "0");//单价
            voucherItem.put("currencycode", "CNY");//货币代码
            voucherItem.put("exchrate", "1.000000");//汇率
            voucherItem.put("orgnd", item[3] != null ? item[3].toString() : "");//借方原币
            voucherItem.put("orgnc", item[4] != null ? item[4].toString() : "");//贷方原币
            voucherItem.put("settlementcode", "");//结算方式代码
            voucherItem.put("settlementno", "");//结算号
            voucherItem.put("bizdate", item[11] != null ? item[11] : "");//业务日期
            String digest = null;
            if (voucherItemDataList != null)
                digest = voucherItemDataList.get(order - 1).getDigest();
            if (digest != null && !digest.isEmpty())
                voucherItem.put("digest", digest);//摘要
            else
                voucherItem.put("digest", item[0] != null ? item[0] : "");//摘要
            voucherItem.put("bizno", "");//业务号
            voucherItem.put("suppliercode", item[8] != null ? item[8] : "");//供应商代码
            voucherItem.put("departmentcode", "");//部门代码
            voucherItem.put("customercode", item[7] != null ? item[7] : "");//客户代码
            voucherItem.put("staffcode", "");//职员代码
            items.add(voucherItem);
            order++;
        }
        result.put("items", items);
        return result;
    }

    @Transactional
    public Map getVoucherMaster(Integer id, Integer attachment, Integer voucherNum, String uuid, String year, String mouth, Map userInfo) {
        Map<String, Object> voucherMaster = new HashMap<String, Object>();
        List result = receiptPaymentDao.getVoucherMaster(id);
        if (result == null) {
            throw new RuntimeException("没该公司科目代码对照信息!");
        }
        voucherMaster.put("id", uuid);
        voucherMaster.put("vchrtypecode", result.get(0));//凭证类别
        voucherMaster.put("vchrnum", voucherNum);//凭证类别
        voucherMaster.put("acctyear", year != null ? year : "");//会计年度
        voucherMaster.put("acctperiod", mouth != null ? mouth : "");//会计期间
        voucherMaster.put("createdate", new Date());//凭证日期
        voucherMaster.put("unitcode", result.get(2));//组织机构代码
        voucherMaster.put("acctbookcode", "01");//账薄标识
        voucherMaster.put("createuser", userInfo.get(AuthInterceptor.USER_NAME_SESSION_KEY));//制单人
        voucherMaster.put("attachment", attachment);//附件张数
        voucherMaster.put("memo", "");//备注
        return voucherMaster;
    }

    @Transactional
    public Map getRevokeInfo(Integer id) {
        Map<String, Object> revokeInfo = new HashMap<String, Object>();
        List result = receiptPaymentDao.getRevokeInfo(id);
        if (result == null) {
            throw new RuntimeException("没该公司的对照信息!");
        }
        revokeInfo.put("id", result.get(0));//凭证ID
        revokeInfo.put("vchrid", result.get(1));//凭证ID
        revokeInfo.put("unitcode", result.get(2));//单位代码
        revokeInfo.put("acctyear", result.get(3));//会计年度
        return revokeInfo;
    }

    @Autowired
    public void setReceiptPaymentDao(ReceiptPaymentDao receiptPaymentDao) {
        this.receiptPaymentDao = receiptPaymentDao;
    }
}
